import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
import Confirm from '#cpt/temp/Confirm'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 					= $Lazy.load(()=>import('#tp/page/page-container'))
const TableComponent 		= $Lazy.state(()=>import('./cpt/table-component'))
const $cache 				= $Lazy.file(()=>import('#private/cache-api'))

const statusData = [{name:'正常',value:'45802'},{name:'作废',value:'45801'}]
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		selectedRows:[],
		// 搜索表单列表
		forms : [
			{ label: '账期',				name: 'summary_sn', 		type:'select', 		loading:true },
			{ label: '区域名称',			name: 'region_id', 			type: 'select', 	loading:true },
			{ label: '客户名称',			name: 'hosp_id', 			type: 'select', 	loading:true },
			{ label: '结算单状态',		name: 'status', 			type: 'select', 	data:statusData },
			// { label: '账单创建日期',		names: ['bill_start_at','bill_end_at'], name:'time-null', type:'date-range' },
			{ label: '账单结算日期',		names: ['bill_start_at','bill_end_at'], name:'time-null', type:'date-range' },
		]
	}
	
	model = {}
	
	componentDidMount(){
		this.getSelect()
		
		if(window.isInit){
			this.fetch()
		}
	}
	// 获取下拉数据
	getSelect = () => {
		const { forms } = this.state
		
		$cache.then( async f => {
			// 账期
			$fn.setRow(forms, 'summary_sn', await f.getCache({ api:'bill-summary/select' } ) ) 
			// 客户名称
			$fn.setRow(forms, 'hosp_id', await f.getCache({ api:'hospital' } ) ) 
			// 区域名称
			$fn.setRow(forms, 'region_id', await f.getCache({ api:'bs_area' } ) ) 
			
			this.setState({ forms })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'bill-summary/index',{isIndex:true})
	
	// 表单
	cols = [
		{ title: '区域名称',		dataIndex: 'region_name', 		width:150 },
		{ title: '客户名称', 	dataIndex: 'hosp_name', 		width:200 },
		{ title: '账期', 		dataIndex: 'summary_sn', 		...$fn.table.num },
		{ title: '标本数量', 	dataIndex: 'spec_num', 			...$fn.table.num },
		{ title: '标准总价', 	dataIndex: 'sprice', 			...$fn.table.num },
		{ title: '折扣率', 		dataIndex: 'percent', 			...$fn.table.num },
		{ title: '实际总价', 	dataIndex: 'price', 			...$fn.table.num},
		// 新增初始结算价字段 20211018 耿琰
		{ title: '初始结算价',	dataIndex: 'initial_price', 	...$fn.table.num },
		{ title: '结算时间', 	dataIndex: 'created_at', 		...$fn.table.time},
		{ title: '结算人', 		dataIndex: 'operator_name', 	...$fn.table.name},
		{ title: '结算单状态', 	dataIndex: 'status', 			...$fn.table.status, render:text=>$fn.getState(statusData,+text) },
	]
	// 操作按钮
	controls = () => {
		const { selectedRows } = this.state
		let hosp_ids = []
		selectedRows.forEach(v=>hosp_ids.push(v.hosp_id))
		this.model?.hosp_id && hosp_ids.push(this.model.hosp_id)
		const param = {summary_sn: this.model.summary_sn, hosp_ids: hosp_ids.join(',')}
		return [
			Confirm.call(this, '反结算', 'bill-hosp/resettlement',{ isKeys:true, query: param}),					// 结算		
			Controls.exports.call(this, 'bill/endexport', {isKeys:true, query:{...this.model,...param}, label:'客户账单导出', code:'F2', isSock:true} ),		// 导出
		]
	}
	render(){
		return (
			<>
				<Page title='已结算账单查询' nobc controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					<div className='bcf fv ex'>
						{ Table.call(this,{isIndex:true,isInit: window.isInit,onRow:( rows ) => this.tableRef.load(rows), idStr:'hosp_id', split:true }) }
					</div>
					{/* 表格 */}
					<div className='bcf fv mt5 ex pt10'>
						<TableComponent onRef={ ref => this.tableRef = ref }/>
					</div>
				</Page>
			</>
		)
	}
}

export default Index